<?php

namespace App\Admin\Controllers;

use App\Models\Banner;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class BannersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '轮播图';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Banner);

        $grid->column('id', __('Id'));
        $grid->column('type', __('banner类型'))->editable('select', Banner::TYPE);
        $grid->column('url', __('banner图'))->image('', 100, 100);
        //$grid->column('link', __('链接地址'));
        //$grid->column('link_type', __('链接类型'));
        $states = [
            'on'  => ['value' => 1, 'text' => '显示', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '隐藏', 'color' => 'default'],
        ];
        $grid->column('isshow', __('显示'))->switch($states);
        $grid->column('sort', __('排序'))->editable();
        $grid->actions(function ($actions) {
            //$actions->disableDelete();
            $actions->disableView();
        });
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->equal('type','类型')->radio(Banner::TYPE);
        });
        $grid->model()->orderBy('id','desc');
        $grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Banner::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('type', __('Type'));
        $show->field('product_id', __('Product id'));
        $show->field('url', __('Url'));
        $show->field('title', __('Title'));
        $show->field('link', __('Link'));
        $show->field('link_type', __('Link type'));
        $show->field('isshow', __('Isshow'));
        $show->field('sort', __('Sort'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Banner);

        $form->select('type', __('类型'))->options(Banner::TYPE)->rules('required');
        $form->number('product_id', __('商品ID'));
        $form->image('url', __('图片上传'))->move('banners')->name(function($file){
            return uniqid().'.'.$file->guessExtension();
        });;
        $form->text('link', __('链接地址'));
        $form->select('link_type', __('链接类型'))->options(Banner::LINK_type)->default(4);
        $states = [
            'on'  => ['value' => 1, 'text' => '显示', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '隐藏', 'color' => 'default'],
        ];
        $form->switch('isshow', __('显示'))->states($states);
        $form->number('sort', __('排序'))->default(0);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });
        return $form;
    }
}
